package com.tanhua.server;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Service
public class SettingsService {
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private BlackListApi blackListApi;
    @Autowired
    private OssTemplate ossTemplate;
    @DubboReference
    private UserInfoApi userInfoApi;


    public SettingsVo getSettings() {
        SettingsVo settingsVo = new SettingsVo();
        Long userId = UserHolder.getUserId();
        Question question = questionApi.getByUserId(userId);
        if (question == null) {
            question = new Question();
            question.setTxt("你喜欢java吗？");
        }
        String mobile = UserHolder.getMobile();
        settingsVo.setStrangerQuestion(question.getTxt());
        settingsVo.setId(userId);
        settingsVo.setPhone(mobile);

        Settings settings = settingsApi.getByUserId(userId);
        settingsVo.setId(userId);
        if (settings != null) {
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }
        return settingsVo;
    }

    public void updateQuestions(String content, Long userId) {
        Question question = questionApi.getByUserId(userId);
        if (question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            question.setTxt(content);
            questionApi.updateQuestions(question);
        }
    }

    public void updateNotification(Settings settings) {
        Settings settings1 = settingsApi.getByUserId(settings.getUserId());
        if (settings1 == null) {
            settingsApi.save(settings);
        } else {
            settings.setId(settings1.getId());
            settingsApi.update(settings);
        }

    }

    public PageResult getBlackList(int page, int pagesize) {
        Long userId = UserHolder.getUserId();
        return blackListApi.getBlackList(page, pagesize, userId);
    }

    public void deleteBlackList(Long blackUserId) {
        Long userId = UserHolder.getUserId();
        blackListApi.deleteBlackList(userId,blackUserId);
    }


}
